//
//  AppDelegate.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/15.
//

#import "AppDelegate.h"
#import "FPLoginController.h"
#import "FPPrivacyController.h"
#import <IQKeyboardManager/IQKeyboardManager.h>

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    
    [self.window makeKeyAndVisible];

    [self configKeybord];
    
//    [AliyunFaceAuthFacade init];// 初始化SDK。
    
    [self setRootController];    
    
    // ALI_ACCESSKEYID
    // ALI_SECRET
    [[TXCommonHandler sharedInstance] setAuthSDKInfo:PNSATAUTHSDKINFO complete:^(NSDictionary * _Nonnull resultDic) {
        
        NSLog(@"设置秘钥结果：%@", resultDic);
    }];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeSourceType:) name:XYSourceTypeDidChangeNotification object:nil];
    
    return YES;
}



- (void)setRootController {
    
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:FIRST_LOGIN_FILE_PATH];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

    
#define LOGIN_STYLE 1

    
#if LOGIN_STYLE
   
    if (dict) {
        NSLog(@"读取到的数据: %@", dict);
        
//        NSString *val = [dict objectForKey:PRIVACY_CONTROLLER];
//        NSLog(@"%@",val);
        self.window.rootViewController = [[FPMainTabBarController alloc] initWithContext:nil sourcetype:@"userInfo.platformType"];
        
    }
    else {
        
        NSLog(@"userData.plist 文件不存在或无法读取");
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[FPPrivacyController new]];
    }
    
#else
    
    if (dict) {
        NSLog(@"读取到的数据: %@", dict);
        
//        NSString *val = [dict objectForKey:PRIVACY_CONTROLLER];
//        NSLog(@"%@",val);
        
        if (FPUserModel.globalToken) {
            
            self.window.rootViewController = [[FPMainTabBarController alloc] initWithContext:nil sourcetype:@"userInfo.platformType"];
        }
        else {
            
            self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[FPLoginController new]];
        }
    }
    else {
        
        NSLog(@"userData.plist 文件不存在或无法读取");
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[FPPrivacyController new]];
    }
    
#endif
}

- (void)configKeybord {
    
//    IQKeyboardManager *keyboardManager = [IQKeyboardManager sharedManager]; // 获取类库的单例变量
//    [keyboardManager setKeyboardDistanceFromTextField:0];
//    keyboardManager.enable = YES; // 控制整个功能是否启用
//    keyboardManager.shouldResignOnTouchOutside = YES; // 控制点击背景是否收起键盘
//    keyboardManager.shouldToolbarUsesTextFieldTintColor = YES; // 控制键盘上的工具条文字颜色是否用户自定义
//    keyboardManager.toolbarManageBehaviour = IQAutoToolbarBySubviews; // 有多个输入框时，可以通过点击Toolbar 上的“前一个”“后一个”按钮来实现移动到不同的输入框
//    keyboardManager.enableAutoToolbar = YES; // 控制是否显示键盘上的工具条
//    //    keyboardManager.shouldShowTextFieldPlaceholder = YES; // 是否显示占位文字
//    keyboardManager.shouldShowToolbarPlaceholder = YES; // 是否显示占位文字
//    keyboardManager.placeholderFont = [UIFont boldSystemFontOfSize:14.f]; // 设置占位文字的字体
//    keyboardManager.keyboardDistanceFromTextField = 10.0f; // 输入框距离键盘的距离
//    //      http://www.jianshu.com/p/77d2ba56914
    
    //设置键盘管理
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [IQKeyboardManager sharedManager].shouldToolbarUsesTextFieldTintColor = YES;
//    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES; // 控制是否显示键盘上的工具条
    
    [[IQKeyboardManager sharedManager] setLayoutIfNeededOnUpdate:YES];
//    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO]; // 如果不需要自动工具栏
    [[IQKeyboardManager sharedManager] setToolbarDoneBarButtonItemText:@"完成"];
}



#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"FinanceProject"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}




@end
